package com.StarStudio.DAO;


import com.ruoyi.common.core.Entity.GoodsStock;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
@SuppressWarnings("all")
public interface GoodsStockDAO {

    List<GoodsStock> findGoodsStockList(GoodsStock goodsStock);
    List<GoodsStock> findGoodsStockListForNormalUser(@Param("goodsStock") GoodsStock goodsStock,@Param("store_id") long store_id);

    @Insert("insert into goods_item values (#{item_id},#{goods_id},#{store_id},#{goods_name},#{have_been_sold},#{sold_order_id},#{add_time},#{sold_time},#{del_flag})")
    void addGoodsStock(GoodsStock goodsStock);

    @Delete("delete from goods_item where item_id = #{id}")
    void deleteGoodsStock(int id);

    @Select("select item_id from goods_item where goods_id = #{goods_id} and store_id = #{store_id} and have_been_sold = false")
    List<Long> findGoodsStockIds(@Param("goods_id") long goods_id, @Param("store_id") long store_id);

    @Select("select count(*) from goods_item where del_flag = false")
    long findAllRecordNumber();

    @Select("select * from goods_item where goods_id = #{goods_id} and have_been_sold = #{have_been_sold}")
    List<GoodsStock> findUnsoldItems(@Param("goods_id") long goods_id,@Param("have_been_sold") boolean have_been_sold);

    @Select("select * from goods_item where goods_id = #{goods_id} and have_been_sold = #{have_been_sold}")
    List<GoodsStock> findSoldItems(@Param("goods_id") long goods_id,@Param("have_been_sold") boolean have_been_sold);

    int updateGoodsStock(GoodsStock goodsStock);

    @Select("select * from goods_item where sold_order_id = #{order_id}")
    List<GoodsStock> findByOrder_id(long order_id);

    @Select("select item_id from goods_item where goods_id = #{goods_id} and have_been_sold = false")
    List<Long> findUnsoldItemIdsByGoodsId(long goods_id);

    @Update("update goods_item set del_flag = true where item_id = #{id}")
    void updateDelFlag(long id);

    List<Long> findSoldItemIdsByGoodsId(long id);

    @Delete("delete from goods_item where item_id = #{id}")
    void realDelete(long id);

    @Select("select * from goods_item where goods_id = #{goods_id} and have_been_sold = #{have_been_sold} limit 1")
    GoodsStock findSoldItem(@Param("goods_id")Long goods_id,@Param("have_been_sold") boolean have_been_sold);

    @Update("update goods_item set have_been_sold = true, sold_order_id = #{order_id}, sold_time = now() where goods_id = #{goods_id} and have_been_sold = false limit 1")
    void updateGoodsStockByOrder(@Param("order_id") long order_id, @Param("goods_id")long goods_id);

    @Select("select * from goods_item where goods_id = #{goods_id} and sold_order_id = #{order_id} limit 1")
    GoodsStock findGoodsStockByGoodsIdAndOrderId(@Param("goods_id")long goods_id, @Param("order_id") long order_id); // 获取抢购的商品个体(前提:每个订单只抢购一件商品)

    @Select("select count(*) from goods_item where store_id = #{storeId} and del_flag = false")
    long findStoreAllRecordNumber(Long storeId);

    @Update("update goods_item set goods_name = #{goodsName} where goods_id = #{goodsId}")
    void updateGoodsName(@Param("goodsName") String goodsName,@Param("goodsId") Long goodsId);
}
